﻿using OpenTK.Input;
using OpenTK;

namespace Painter3D.Tool
{
    class FPSCameraControl : Component
    {
        public float lineSpeed;
        public float angleSpeed;

        private int lastMousePosX;
        private int lastMousePosY;
        private InputManager.UpTriger triger = new InputManager.UpTriger();


        private float pitch = 0.0f;
        private float yaw = 0.0f;
        private bool first = true;

        public bool antiControl = false;
        bool cursorVisable = false;


        public FPSCameraControl(float lineSpeed, float angleSpeed, Transform transform) : base(transform)
        {
            this.lineSpeed = lineSpeed;
            this.angleSpeed = angleSpeed;
            SwitchMode();
        }

        public FPSCameraControl(Transform transform) : this(10, 6, transform)
        {

        }

        protected override void Loop()
        {
            if (first)
            {
                lastMousePosX = InputManager.mouse.X;
                lastMousePosY = InputManager.mouse.Y;
                first = false;
            }
            if (InputManager.keyboard.IsKeyDown(Key.W))
            {
                transform.Translate(Vector3.UnitZ * InputManager.timeStep * -lineSpeed);
            }
            else if (InputManager.keyboard.IsKeyDown(Key.S))
            {
                transform.Translate(Vector3.UnitZ * InputManager.timeStep * lineSpeed);
            }
            else if (InputManager.keyboard.IsKeyDown(Key.A))
            {
                transform.Translate(Vector3.UnitX * InputManager.timeStep * -lineSpeed);
            }
            else if (InputManager.keyboard.IsKeyDown(Key.D))
            {
                transform.Translate(Vector3.UnitX * InputManager.timeStep * lineSpeed);
            }

            if (triger.Sign(InputManager.keyboard.IsKeyDown(Key.Tab)))
            {
                cursorVisable = !cursorVisable;
                SwitchMode();
            }

            if (!cursorVisable)
            {
                int diffX = InputManager.mouse.X - lastMousePosX;
                int diffY = InputManager.mouse.Y - lastMousePosY;
                if (antiControl)
                {
                    pitch += diffY * InputManager.timeStep * angleSpeed;
                    yaw += diffX * InputManager.timeStep * angleSpeed;
                }
                else
                {
                    pitch -= diffY * InputManager.timeStep * angleSpeed;
                    yaw -= diffX * InputManager.timeStep * angleSpeed;
                }
                transform.quaternion = Quaternion.Identity;
                transform.Rotate(Vector3.UnitY, yaw);
                transform.Rotate(Vector3.UnitX, pitch);
            }
            lastMousePosX = InputManager.mouse.X;
            lastMousePosY = InputManager.mouse.Y;
        }

        private void SwitchMode()
        {
            Window.windowConfig.window.CursorVisible = cursorVisable;
            Window.windowConfig.window.CursorGrabbed = !cursorVisable;
        }
    }
}
